package com.zjw.spring.mvc;

import com.zjw.spring.mvc.servlet.DispatcherServlet;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;

public class Application {

    public static void run(Class<?> app, String[] args) {
        String packageName = app.getPackage().getName();
        Tomcat tomcat=new Tomcat();
        Connector connector=new Connector();
        connector.setURIEncoding("UTF-8");
        connector.setPort(8080);
        tomcat.getService().addConnector(connector);

        Context context=tomcat.addContext("/",null);
        Wrapper wrapper = Tomcat.addServlet(context, "dispatcherServlet", new DispatcherServlet());
        wrapper.addInitParameter("packageName", packageName);
        context.addServletMappingDecoded("/","dispatcherServlet");

        try {
            tomcat.start();
            tomcat.getServer().await();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }
    }

}
